package com.peanut.baby.comm;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.http.HttpConfig;
import com.peanut.baby.model.Advice;
import com.peanut.baby.model.InitialAdTK;
import com.peanut.baby.model.UStatus;
import com.peanut.baby.mvp.web.WebViewActivity;
import com.peanut.baby.util.AppUtil;
import com.peanut.baby.util.PrefUtil;
import com.peanut.baby.widget.NoLineClickSpan;
import com.peanut.devlibrary.BaseActivity;
import com.peanut.devlibrary.imageloader.ImageLoader;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.util.SysUtil;
import com.tencent.mars.xlog.Log;

import java.util.Arrays;
import java.util.List;

import cn.carbswang.android.numberpickerview.library.NumberPickerView;

public class BaseDialog {
    private static final String TAG = BaseDialog.class.getSimpleName();
    private static final int DIAOLOG_MARGIN = 80;


    public BaseDialog() {

    }


    /**
     * 显示俩
     *
     * @param context
     * @param contentStr     内容  必传
     * @param okBtnStr       确定按键名称		默认 “确定”
     * @param cancelBtnStr   取消按键名称		默认 “取消”
     * @param okListener     确定按键回调
     * @param cancelListener 取消按键回调
     * @return
     */
    public synchronized AlertDialog showConfirmCancelAlert(final Context context,String contentStr,
                                                           String okBtnStr,
                                                           String cancelBtnStr,
                                                           final OnClickListener okListener,
                                                           final OnClickListener cancelListener) {

        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return null;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog dialog = builder.create();
        dialog.show();
        dialog.setCancelable(false);
        final Window window = dialog.getWindow();
        // 调整dialog大小
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels - SysUtil.dip2px(DIAOLOG_MARGIN);
        window.setAttributes(params);

        window.setContentView(R.layout.dialog_cancel_confirm);

        TextView msg = (TextView) window
                .findViewById(R.id.dialog_msg);
        TextView ok = (TextView) window
                .findViewById(R.id.dialog_ok);
        TextView cancel = (TextView) window
                .findViewById(R.id.dialog_cancel);


        msg.setText(contentStr);
        if (!StringUtil.isNullOrEmpty(okBtnStr)) {
            ok.setText(okBtnStr);
        }

        if (!StringUtil.isNullOrEmpty(cancelBtnStr)) {
            cancel.setText(cancelBtnStr);
        }

        ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (dialog != null) {
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                }
                if (okListener != null) {
                    okListener.onClick(arg0);
                }

            }
        });

        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (dialog != null) {
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                }
                if (cancelListener != null) {
                    cancelListener.onClick(arg0);
                }
            }
        });

        return dialog;
    }

    /**
     * 显示俩按键dialog
     *
     * @param context
     * @param contentStr 内容  必传
     * @param okBtnStr   确定按键名称		默认 “确定”
     * @param okListener 确定按键回调
     * @return
     */
    public AlertDialog showConfirmAlert(final Context context,
                                        String contentStr,
                                        String okBtnStr,
                                        final OnClickListener okListener) {

        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return null;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog dialog = builder.create();
        dialog.show();
        dialog.setCancelable(false);
        final Window window = dialog.getWindow();
        // 调整dialog大小
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels - SysUtil.dip2px(DIAOLOG_MARGIN);
        window.setAttributes(params);

        window.setContentView(R.layout.dialog_confirm);

        TextView msg = (TextView) window
                .findViewById(R.id.dialog_msg);
        TextView ok = (TextView) window
                .findViewById(R.id.dialog_ok);



        msg.setText(contentStr);
        if (!StringUtil.isNullOrEmpty(okBtnStr)) {
            ok.setText(okBtnStr);
        }


        ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (dialog != null) {
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                }
                if (okListener != null) {
                    okListener.onClick(arg0);
                }

            }
        });


        return dialog;
    }

    /**
     *
     * @param context
     * @param listener
     * @return
     */
    public Dialog showMainMenuDialog(final Context context, final OnMainMenuItemClicked listener) {

        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return null;
        }

        final Dialog dialog = new Dialog(context,R.style.dialog_full_width);
        dialog.setContentView(R.layout.dialog_main_menu);
        dialog.show();
        final Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setContentView(R.layout.dialog_main_menu);

        window.findViewById(R.id.plus_close).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
            }
        });
        window.findViewById(R.id.plus_record).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onMainMenuItemClicked(OnMainMenuItemClicked.TYPE_RECORD);
                }
            }
        });
        window.findViewById(R.id.plus_bbs).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onMainMenuItemClicked(OnMainMenuItemClicked.TYPE_BBS);
                }
            }
        });
        window.findViewById(R.id.plus_ask).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onMainMenuItemClicked(OnMainMenuItemClicked.TYPE_ASK);
                }
            }
        });
        window.findViewById(R.id.plus_search).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onMainMenuItemClicked(OnMainMenuItemClicked.TYPE_SEARCH);
                }
            }
        });

        return dialog;
    }

    /**
     *
     * @param context
     * @param listener
     * @return
     */
    public Dialog showShareDialog(final Context context, final OnShareItemSelected listener) {

        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return null;
        }

        final Dialog dialog = new Dialog(context,R.style.dialog_full_width);
        dialog.setContentView(R.layout.dialog_share);
        dialog.show();
        final Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setContentView(R.layout.dialog_share);


        window.findViewById(R.id.share_weixin).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onShareItemSelected(OnShareItemSelected.TYPE_WEIXIN);
                }
            }
        });
        window.findViewById(R.id.share_moment).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onShareItemSelected(OnShareItemSelected.TYPE_MOMENT);
                }
            }
        });
        window.findViewById(R.id.share_qq).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onShareItemSelected(OnShareItemSelected.TYPE_QQ);
                }
            }
        });
        window.findViewById(R.id.share_weibo).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onShareItemSelected(OnShareItemSelected.TYPE_WEIBO);
                }
            }
        });
        window.findViewById(R.id.share_bg).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
            }
        });

        return dialog;
    }

    public Dialog showTKDialog(final Context context, InitialAdTK tk) {

        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return null;
        }

        final Dialog dialog = new Dialog(context,R.style.dialog_full_width);
        dialog.setContentView(R.layout.dialog_tk);
        dialog.show();
        final Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setContentView(R.layout.dialog_tk);


        RelativeLayout shareBg = window.findViewById(R.id.share_bg);

        ViewGroup.LayoutParams params1 = shareBg.getLayoutParams();
        params1.width = InitManager.getInstance().getScreenWidth();
        shareBg.setLayoutParams(params1);

        FrameLayout frameLayout = window.findViewById(R.id.tk_frame);
        ImageView image = window.findViewById(R.id.tk_image);

        int width = InitManager.getInstance().getScreenWidth() - SysUtil.dip2px(60);
        int height = width * 4 / 3;

        RelativeLayout.LayoutParams fParams = (RelativeLayout.LayoutParams) frameLayout.getLayoutParams();
        int height1 = height + SysUtil.dip2px(15);
        fParams.width = width;
        fParams.height = height1;
        frameLayout.setLayoutParams(fParams);


        ImageLoader.loadImageView(context,tk.imgUrl,image);

        shareBg.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PrefUtil.saveBoolean("TK_"+ tk.id,true);
                dismissDialog(dialog);
                WebViewActivity.start(context,tk.htmlUrl,tk.name,"");
            }
        });

        ImageView close  = window.findViewById(R.id.tk_close);
        close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                PrefUtil.saveBoolean("TK_"+ tk.id,true);
                dismissDialog(dialog);
            }
        });
        return dialog;
    }



    /**
     * @param context
     * @param contentStr
     * @param okBtnStr
     * @param autoDismiss
     * @param okListener
     * @return
     */
    public AlertDialog showConfirmAlert(final Context context,
                                        String contentStr,
                                        String okBtnStr, final boolean autoDismiss,
                                        final OnClickListener okListener) {

        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return null;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog dialog = builder.create();
        dialog.show();
        dialog.setCancelable(false);
        final Window window = dialog.getWindow();
        // 调整dialog大小
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels - SysUtil.dip2px(DIAOLOG_MARGIN);
        window.setAttributes(params);

        window.setContentView(R.layout.dialog_confirm);

        TextView msg = (TextView) window
                .findViewById(R.id.dialog_msg);
        TextView ok = (TextView) window
                .findViewById(R.id.dialog_ok);


        msg.setText(contentStr);
        if (!StringUtil.isNullOrEmpty(okBtnStr)) {
            ok.setText(okBtnStr);
        }


        ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (autoDismiss) {
                    if (dialog != null) {
                        if (dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                }

                if (okListener != null) {
                    okListener.onClick(arg0);
                }

            }
        });


        return dialog;
    }

    /**
     *
     * @param context
     * @param okListener
     * @return
     */
    public AlertDialog showPrivacyDialog(final Context context,
                                        final OnClickListener okListener,final OnClickListener cancelListener) {

        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return null;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog dialog = builder.create();
        dialog.show();
        dialog.setCancelable(false);
        final Window window = dialog.getWindow();
        // 调整dialog大小
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels - SysUtil.dip2px(DIAOLOG_MARGIN);
        window.setAttributes(params);

        window.setContentView(R.layout.dialog_privacy);

        TextView msg = (TextView) window
                .findViewById(R.id.dialog_msg);
        TextView ok = (TextView) window
                .findViewById(R.id.dialog_ok);
        TextView cancel = (TextView) window
                .findViewById(R.id.dialog_cancel);

        String text = "（1）我们收集的个人信息，仅用于提供更好的服务。我们一直致力于保护用户的隐私信息安全。\n\n" +
                "（2）如果您不同意隐私政策条款，我们将无法为您提供服务。\n\n" +
                "请阅读完整的《软件使用及服务协议》和《隐私政策及声明》。\n";
        SpannableStringBuilder builder1 = new SpannableStringBuilder(text);

        NoLineClickSpan span1 = new NoLineClickSpan(context.getResources().getColor(R.color.colorPrimary), new NoLineClickSpan.OnSpanClickedListener() {
            @Override
            public void processClick() {
                Log.d(TAG,"process click1");
                WebViewActivity.start(context, HttpConfig.TERMS_POLICY_URL,"软件使用及服务协议","");
            }
        });
        NoLineClickSpan span2 = new NoLineClickSpan(context.getResources().getColor(R.color.colorPrimary), new NoLineClickSpan.OnSpanClickedListener() {
            @Override
            public void processClick() {
                Log.d(TAG,"process click2 "+HttpConfig.PRIVACY_POLICY_URL);
                WebViewActivity.start(context, HttpConfig.PRIVACY_POLICY_URL,"隐私政策及声明","");
            }
        });

//        ClickableSpan span1 = new ClickableSpan() {
//            @Override
//            public void onClick(@NonNull View widget) {
//                Log.d(TAG,"process click1");
//                WebViewActivity.start(context, HttpConfig.TERMS_POLICY_URL,"软件使用及服务协议","");
//            }
//        };
//        ClickableSpan span2 = new ClickableSpan() {
//            @Override
//            public void onClick(@NonNull View widget) {
//                Log.d(TAG,"process click1");
//                WebViewActivity.start(context, HttpConfig.POLICYY_URL,"隐私政策及声明","");
//            }
//        };
        builder1.setSpan(span1,81,92, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        builder1.setSpan(span2,93,102, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        msg.setText(builder1);
        msg.setMovementMethod(LinkMovementMethod.getInstance());

        ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismissDialog(dialog);
                if (okListener != null) {
                    okListener.onClick(arg0);
                }

            }
        });

        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismissDialog(dialog);
                if (cancelListener != null) {
                    cancelListener.onClick(arg0);
                }

            }
        });


        return dialog;
    }


    /**
     *
     * @param context
     * @param okListener
     * @return
     */
    public AlertDialog showWenxiTishi(final Context context,
                                         final OnClickListener okListener,final OnClickListener cancelListener) {

        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return null;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final AlertDialog dialog = builder.create();
        dialog.show();
        dialog.setCancelable(false);
        final Window window = dialog.getWindow();
        // 调整dialog大小
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels - SysUtil.dip2px(DIAOLOG_MARGIN);
        window.setAttributes(params);

        window.setContentView(R.layout.dialog_wenxintishi);

        TextView msg = (TextView) window
                .findViewById(R.id.dialog_msg);
        TextView ok = (TextView) window
                .findViewById(R.id.dialog_ok);
        TextView cancel = (TextView) window
                .findViewById(R.id.dialog_cancel);

        String text = "感谢您的信任并使用本软件。请您查阅《软件使用及服务协议》和《隐私政策及声明》。\n" +
                "我们非常重视您的的隐私。我们会根据本软件的功能收集个人信息，我们不会向第三方提供您的信息，除非得到您的授权。\n" +
                "同意并接受以上两个文件的条款后即可开始使用本软件。\n";
        SpannableStringBuilder builder1 = new SpannableStringBuilder(text);

        NoLineClickSpan span1 = new NoLineClickSpan(context.getResources().getColor(R.color.colorPrimary), new NoLineClickSpan.OnSpanClickedListener() {
            @Override
            public void processClick() {
                Log.d(TAG,"process click1");
                WebViewActivity.start(context, HttpConfig.TERMS_POLICY_URL,"软件使用及服务协议","");
            }
        });
        NoLineClickSpan span2 = new NoLineClickSpan(context.getResources().getColor(R.color.colorPrimary), new NoLineClickSpan.OnSpanClickedListener() {
            @Override
            public void processClick() {
                Log.d(TAG,"process click2");
                WebViewActivity.start(context, HttpConfig.PRIVACY_POLICY_URL,"隐私政策及声明","");
            }
        });

//        ClickableSpan span1 = new ClickableSpan() {
//            @Override
//            public void onClick(@NonNull View widget) {
//                Log.d(TAG,"process click1");
//                WebViewActivity.start(context, HttpConfig.TERMS_POLICY_URL,"软件使用及服务协议","");
//            }
//        };
//        ClickableSpan span2 = new ClickableSpan() {
//            @Override
//            public void onClick(@NonNull View widget) {
//                Log.d(TAG,"process click1");
//                WebViewActivity.start(context, HttpConfig.POLICYY_URL,"隐私政策及声明","");
//            }
//        };
        builder1.setSpan(span1,17,28, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        builder1.setSpan(span2,29,38, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

        msg.setText(builder1);
        msg.setMovementMethod(LinkMovementMethod.getInstance());

        ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismissDialog(dialog);

                if (okListener != null) {
                    okListener.onClick(arg0);
                }

            }
        });

        cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismissDialog(dialog);
                if (cancelListener != null) {
                    cancelListener.onClick(arg0);
                }

            }
        });


        return dialog;
    }



    /**
     * 头像选择dialog
     *
     * @param act
     * @param cameraOnClickListener
     * @param albumOnClickListener
     */
    public void showAvatarSelectDialog(BaseActivity act, final OnClickListener cameraOnClickListener, final OnClickListener albumOnClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(act);
        final AlertDialog dialog = builder.create();
        dialog.show();
        final Window window = dialog.getWindow();

        WindowManager.LayoutParams params = window.getAttributes();
        params.width = InitManager.getInstance().getCtx().getResources().getDisplayMetrics().widthPixels - SysUtil.dip2px(DIAOLOG_MARGIN);
        window.setAttributes(params);
        window.setContentView(R.layout.dialog_select_avatar);

        TextView camera = (TextView) window
                .findViewById(R.id.avatar_camera);
        TextView album = (TextView) window
                .findViewById(R.id.avatar_album);


        camera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismissDialog(dialog);
                if (cameraOnClickListener != null) {
                    cameraOnClickListener.onClick(arg0);
                }

            }
        });


        album.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismissDialog(dialog);
                if (albumOnClickListener != null) {
                    albumOnClickListener.onClick(arg0);
                }

            }
        });

    }

    public void showPhotoSelectDialog(BaseActivity act, final OnClickListener cameraOnClickListener, final OnClickListener albumOnClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(act);
        final AlertDialog dialog = builder.create();
        dialog.show();
        final Window window = dialog.getWindow();

        WindowManager.LayoutParams params = window.getAttributes();
        params.width = InitManager.getInstance().getCtx().getResources().getDisplayMetrics().widthPixels - SysUtil.dip2px(DIAOLOG_MARGIN);
        window.setAttributes(params);
        window.setContentView(R.layout.dialog_select_photo);

        TextView camera = (TextView) window
                .findViewById(R.id.avatar_camera);
        TextView album = (TextView) window
                .findViewById(R.id.avatar_album);

        camera.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismissDialog(dialog);
                if (cameraOnClickListener != null) {
                    cameraOnClickListener.onClick(arg0);
                }

            }
        });


        album.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismissDialog(dialog);
                if (albumOnClickListener != null) {
                    albumOnClickListener.onClick(arg0);
                }

            }
        });

    }


    /**
     * 性别选择dialog
     *
     * @param act
     * @param selectedOnClickListener
     */
    public void showSexDialog(final BaseActivity act, final OnClickListener selectedOnClickListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(act);
        final AlertDialog dialog = builder.create();
        dialog.show();
        final Window window = dialog.getWindow();

        WindowManager.LayoutParams params = window.getAttributes();
        params.width = InitManager.getInstance().getCtx().getResources().getDisplayMetrics().widthPixels - SysUtil.dip2px(DIAOLOG_MARGIN);
        window.setAttributes(params);
        window.setContentView(R.layout.dialog_sex);

        TextView male = (TextView) window
                .findViewById(R.id.sex_male);

        TextView female = (TextView) window
                .findViewById(R.id.sex_female);


        male.setTag(1);
        female.setTag(2);


        male.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismissDialog(dialog);
                if (selectedOnClickListener != null) {
                    selectedOnClickListener.onClick(arg0);
                }

            }
        });

        female.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismissDialog(dialog);
                if (selectedOnClickListener != null) {

                    selectedOnClickListener.onClick(arg0);
                }
            }
        });

    }

    public void showAdviceDialog(final Activity act, Advice advice) {
        AlertDialog.Builder builder = new AlertDialog.Builder(act);
        final AlertDialog dialog = builder.create();
        dialog.show();
        final Window window = dialog.getWindow();

        WindowManager.LayoutParams params = window.getAttributes();
        params.width = InitManager.getInstance().getScreenWidth() - SysUtil.dip2px(DIAOLOG_MARGIN);;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
        window.setBackgroundDrawableResource(R.drawable.shape_dialog_advice_bg);
        window.setContentView(R.layout.dialog_advice);

        TextView rate = (TextView) window.findViewById(R.id.advice_rate);
        TextView tiaoli = (TextView) window.findViewById(R.id.advice_tiaoli);
        TextView tips = (TextView) window.findViewById(R.id.advice_tips);

        rate.setText(StringUtil.isNullOrEmpty(advice.probability)?"未知":advice.probability+"");
        tiaoli.setText(advice.care);
        tips.setText(advice.tips);

    }

    public void showListDialog(final BaseActivity act, String title, final List<String> datasource, final OnListItemSelectedListener selectedListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(act);
        final AlertDialog dialog = builder.create();
        dialog.show();
        final Window window = dialog.getWindow();

        WindowManager.LayoutParams params = window.getAttributes();
        params.width = InitManager.getInstance().getScreenWidth();
        params.gravity = Gravity.CENTER;
//        params.height = params.width / 2;
        window.setAttributes(params);
        window.setContentView(R.layout.dialog_list);
        ((TextView) window.findViewById(R.id.list_title)).setText(title);

        ListView list = (ListView) window.findViewById(R.id.list_list);
        DialogListAdapter adapter = new DialogListAdapter(act,datasource);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String result = datasource.get(position);
                dismissDialog(dialog);
                if(selectedListener != null){
                    selectedListener.onListItemSelected(result);
                }
            }
        });
    }

    public void showUStatusDialog(final BaseActivity act, String title, final OnUStatusSelectedListener selectedListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(act);
        final AlertDialog dialog = builder.create();
        dialog.show();
        final Window window = dialog.getWindow();

        WindowManager.LayoutParams params = window.getAttributes();
        params.width = InitManager.getInstance().getScreenWidth();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
        window.setContentView(R.layout.dialog_list);
        ((TextView) window.findViewById(R.id.list_title)).setText(title);

        ListView list = (ListView) window.findViewById(R.id.list_list);
        final List<UStatus> datasource = AppUtil.getUStatusList();
        UStatusListAdapter adapter = new UStatusListAdapter(act, datasource);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UStatus result = datasource.get(position);
                dismissDialog(dialog);
                if(selectedListener != null){
                    selectedListener.onUStatusSelected(result);
                }
            }
        });
    }

    public Dialog showSinglePickerDialog(final Context context,String title, String[] values,String current,final OnPickerListener listener) {

        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return null;
        }

        final Dialog dialog = new Dialog(context,R.style.dialog_full_width);
        dialog.setContentView(R.layout.dialog_calendar_tongfang);
        dialog.show();
        final Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setContentView(R.layout.dialog_calendar_tongfang);

        ((TextView) window.findViewById(R.id.title)).setText(title);

        final NumberPickerView pickerView = (NumberPickerView) window.findViewById(R.id.picker);
        pickerView.refreshByNewDisplayedValues(values);
        if(!StringUtil.isNullOrEmpty(current)){
            List<String> list = Arrays.asList(values);
            int index = list.indexOf(current);
            pickerView.smoothScrollToValue(index);
        }else{
            pickerView.smoothScrollToValue(0);
        }

        window.findViewById(R.id.clear).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onPickered("");
                }
            }
        });
        window.findViewById(R.id.confirm).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onPickered(pickerView.getContentByCurrValue());
                }
            }
        });
        window.findViewById(R.id.share_bg).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
            }
        });

        return dialog;
    }

    public Dialog showTiWenPickerDialog(final Context context,
                                        String[] values1,
                                        String[] values2,
                                        String current1,
                                        String current2,
                                        final onTiWenPickerListener listener) {
        Log.d(TAG,"current1: "+current1+" current2: "+current2);
        if (context instanceof Activity && ((Activity) context).isFinishing()) {
            return null;
        }

        final Dialog dialog = new Dialog(context,R.style.dialog_full_width);
        dialog.setContentView(R.layout.dialog_calendar_tiwen);
        dialog.show();
        final Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setContentView(R.layout.dialog_calendar_tiwen);

        final NumberPickerView pickerView1 = (NumberPickerView) window.findViewById(R.id.picker1);
        final NumberPickerView pickerView2 = (NumberPickerView) window.findViewById(R.id.picker2);
        pickerView1.refreshByNewDisplayedValues(values1);
        pickerView2.refreshByNewDisplayedValues(values2);

        if(!StringUtil.isNullOrEmpty(current1)){
            List<String> list = Arrays.asList(values1);
            final int index = list.indexOf(current1);
            Log.d(TAG,"index1: "+index);
            pickerView1.post(new Runnable() {
                @Override
                public void run() {
                    pickerView1.smoothScrollToValue(index);
                }
            });

        }else{
            pickerView1.smoothScrollToValue(0);
        }

        if(!StringUtil.isNullOrEmpty(current2)){
            List<String> list = Arrays.asList(values2);
            final int index = list.indexOf(current2);
            Log.d(TAG,"index2: "+index);
            pickerView2.post(new Runnable() {
                @Override
                public void run() {
                    pickerView2.smoothScrollToValue(index);
                }
            });

        }else{
            pickerView2.smoothScrollToValue(0);
        }

        window.findViewById(R.id.clear).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onPickered("","");
                }
            }
        });
        window.findViewById(R.id.confirm).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
                if(listener != null){
                    listener.onPickered(pickerView1.getContentByCurrValue(),pickerView2.getContentByCurrValue());
                }
            }
        });
        window.findViewById(R.id.share_bg).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissDialog(dialog);
            }
        });

        return dialog;
    }


    /**
     * 取消dialog
     *
     * @param dialog
     */
    private void dismissDialog(Dialog dialog) {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }

    public interface OnMainMenuItemClicked{
        int TYPE_RECORD     = 0;
        int TYPE_BBS        = 1;
        int TYPE_ASK        = 2;
        int TYPE_SEARCH     = 3;
        void onMainMenuItemClicked(int item);
    }
    public interface OnShareItemSelected{
        int TYPE_WEIXIN         = 0;
        int TYPE_MOMENT         = 1;
        int TYPE_QQ             = 2;
        int TYPE_WEIBO          = 3;
        void onShareItemSelected(int item);
    }

    public interface OnListItemSelectedListener{
        void onListItemSelected(String value);
    }

    public interface OnUStatusSelectedListener{
        void onUStatusSelected(UStatus status);
    }


    public interface OnPickerListener{
        void onPickered(String value);
    }
    public interface onTiWenPickerListener{
        void onPickered(String value1, String value2);
    }
}
